package com.baidu.baidumap;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import com.baidu.mapapi.overlayutil.PoiOverlay;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiBoundSearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;

/**
 * 范围内搜索
 */
public class PoiSearchInBoundsDemo extends BaseActivity {
    private PoiSearch poiSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        search();
    }

    private void search() {
        poiSearch = PoiSearch.newInstance();
        poiSearch.setOnGetPoiSearchResultListener(new MyListener());

        PoiBoundSearchOption boundSearchOption= new PoiBoundSearchOption();
        LatLngBounds latLngBounds = new LatLngBounds.Builder()
                .include(new LatLng(40.049233, 116.302675))// 东北的点
                .include(new LatLng(40.050645, 116.303695))// 西南的点
                .build();
        boundSearchOption.bound(latLngBounds);//设置搜索范围
        boundSearchOption.keyword("加油站");
        poiSearch.searchInBound(boundSearchOption);

      /*  poiSearch = PoiSearch.newInstance();
        poiSearch.setOnGetPoiSearchResultListener(new MyListener());

        PoiNearbySearchOption nearbyOption = new PoiNearbySearchOption();
        nearbyOption.location(hmPos);// 设置中心点
        nearbyOption.radius(1000);// 设置半径 单位是米
        nearbyOption.keyword("加油站");// 关键字
        poiSearch.searchNearby(nearbyOption);*/
    }

    class MyListener implements OnGetPoiSearchResultListener{



        @Override
        public void onGetPoiResult(PoiResult poiResult) {
            if (poiResult==null|| SearchResult.ERRORNO.RESULT_NOT_FOUND==poiResult.error){
                Toast.makeText(getApplicationContext(),"未搜索到结果",Toast.LENGTH_SHORT).show();
                return;
            }
            PoiOverlay overlay = new MyPoiOverLay(baiduMap);//搜索poi的覆盖物
            baiduMap.setOnMarkerClickListener(overlay);
            overlay.setData(poiResult);
            overlay.addToMap();
            overlay.zoomToSpan();
        }

        @Override
        public void onGetPoiDetailResult(PoiDetailResult result) {

        }


    }

    class MyPoiOverLay extends PoiOverlay{

        public MyPoiOverLay(BaiduMap baiduMap) {
            super(baiduMap);
        }

        @Override
        public boolean onPoiClick(int i) {
            PoiResult poiResult = getPoiResult();
            PoiInfo poiInfo = poiResult.getAllPoi().get(i);
            String text = poiInfo.name+","+poiInfo.address;
           Toast.makeText(getApplicationContext(),text,Toast.LENGTH_SHORT).show();
            return super.onPoiClick(i);
        }
    }
}
